package com.woniuxy.userdemo.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.woniuxy.userdemo.entity.ResponseResult;
import com.woniuxy.userdemo.entity.User;
import com.woniuxy.userdemo.service.IUserService;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("user")
public class UserController {
    //今天是天气不错
    @Autowired
    IUserService userService ;

    @RequestMapping("/login")
    @ResponseBody  // 不走视图解析器
    public ResponseResult<User> login(String username, String password,
                                HttpServletRequest request) throws IOException {
        ResponseResult<User> result = new ResponseResult<>();
        try {
            User user = userService.login(username);
            if (user == null){
                result.setCode(201);
                result.setMsg("账号不存在!");
            }else {
                if (user.getPassword().equals(password)) {
                    request.getSession().setAttribute("userInfo",user);
                    result.setCode(200);
                    result.setMsg("登录成功!");
                    result.setData(user);
                }else {
                    result.setCode(202);
                    result.setMsg("密码错误!");
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!");
        }
        return result ;
    }
    @ResponseBody
    @RequestMapping("/list")
    public ResponseResult<PageInfo<User>> list(String username,
                                               @RequestParam(value = "education",defaultValue = "-1") Integer education ,
                                               @RequestParam(value = "now",defaultValue = "1")Integer now,
                                               @RequestParam(value = "size",defaultValue = "3")Integer size){
        ResponseResult<PageInfo<User>> result = new ResponseResult<>();
        try {
            PageInfo<User> pageInfo = userService.findUserByPage(username,education,now,size);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @ResponseBody
    @RequestMapping("/findById")
    public ResponseResult<User> findById(int id){
        ResponseResult<User> result = new ResponseResult<>();
        try {
            User user = userService.findUserById(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(user);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }
    @ResponseBody
    @RequestMapping("/modify")
    public ResponseResult<Void> modify(User user){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            userService.modify(user);
            result.setCode(200);
            result.setMsg("修改成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败!");
        }
        return result ;
    }

    @ResponseBody
    @RequestMapping("/del")
    public ResponseResult<Void> del(int id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            userService.removeByKey(id);
            result.setCode(200);
            result.setMsg("删除成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败!");
        }
        return result ;
    }

    /*
    userAdd:{
                    username:'',
                    sex:'男',
                    age:'',
                    tel:'',
                    education:-1
                }

                 private String username ;
                private String password ;
                private String sex ;
                private int age ;
                private String tel ;
                private int education ;
     */
    @ResponseBody
    @RequestMapping("/add")
    public ResponseResult<Void> add(User user){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            user.setPassword("123");
            userService.add(user);
            result.setCode(200);
            result.setMsg("保存成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("保存失败!");
        }
        return result ;
    }
    /*@ResponseBody
    @RequestMapping("/add")
    public int ssss(User user){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            user.setPassword("123");
            userService.add(user);
            result.setCode(200);
            result.setMsg("保存成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("保存失败!");
        }
        return 1 ;
    }
*/

    public void test(){
        System.out.println("AAAAAAAAA");
    }
}
